from django.http import JsonResponse
from rest_framework.views import APIView
from app.account.models import UserLeaderVerifyForm
from common.authCheck.userAuth import check_login, getUser
import json


class UserRoleVerifyInfoView(APIView):

    @check_login
    def post(self, request):
        """
        用户提交认证表单
        :param request:
        :return:
        """
        user = getUser(request.session.get('login'))
        if user.user_role == '99':
            return JsonResponse({
                'status': False,
                'errMsg': '你已经是认证领队'
            }, status=401)
        jsonParams = json.loads(request.body.decode('utf-8'))
        if jsonParams.get('description') is None:
            return JsonResponse({
                'status': False,
                'errMsg': '描述内容不能为空'
            })
        verifyForm = UserLeaderVerifyForm.objects.create(
            related_user=user,
            description=jsonParams.get('description')
        )
        return JsonResponse({
            'status': True,
            'verify_form': verifyForm.id
        })

    @check_login
    def put(self, request, fid):
        """
        用于表单上传文件的api
        :param request:
        :param fid:
        :return:
        """
        verifyForm = UserLeaderVerifyForm.objects.filter(id=fid)
        if not verifyForm.exists():
            return JsonResponse({
                'status': False,
                'errMsg': '表单不存在'
            }, status=404)
        verifyForm = verifyForm[0]
        file = request.FILES.get('file')
        verifyForm.supporting_file = file
        verifyForm.save()
        return JsonResponse({
            'status': True,
            'verify_form': fid
        })
